package com.hmdp.config;

import com.hmdp.interceptor.LoginInterceptor;
import com.hmdp.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvnConfig  implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

  /*
  * 两个拦截器很有意思，
  * token就是登录的信息，UserDto，时间到了，就自己退出登录了
  * 第一个看RefreshTokenInterceptor拦截器，是为了保证在首页瞎逛，也会刷新token，所以是拦截所有
  * 第二个拦截器LoginInterceptor是拦截需要登录的路径，你进首页根本不需要登录
  * */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor(stringRedisTemplate))
                .excludePathPatterns(
                        "/shop/**",
                        "/voucher/**",
                        "/shop-type/**",
                        "/upload/**",
                        "/blog/hot",
                        "/user/code",
                        "/user/login"
                ).order(1);
//        这个拦截器先执行 order 0 越小越先执行
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**").order(0);


    }
}
